<?php

/**
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */

if ($_POST) {
    require_once(dirname(__FILE__).'/../config/config.inc.php');
    
    ob_start();
    
    if (Util::is_email($_POST['email'])) {
        $addr = DbGet::miscellaneous('email_service');
        $subject = 'Contato'.$_POST['name'];

        $message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'."\n";
        $message .= '<HTML>'."\n";
        $message .= '    <HEAD>'."\n";
        $message .= '        <META HTTP-EQUIV=\"CONTENT-TYPE\" CONTENT=\"text/html; charset=utf-8\">'."\n";
        $message .= '    </HEAD>'."\n";
        $message .= '    <BODY LANG=\"pt-BR\" DIR=\"LTR\">'."\n";
        $message .= '        <p>Nome: '.$_POST['name'].'</p>'."\n";
        $message .= '        <p>Telefone: '.$_POST['phone'].'</p>'."\n";
        $message .= '        <p>E-mail: '.$_POST['email'].'</p>'."\n";
        $message .= '        <p>Cidade/UF: '.$_POST['city'].'/'.$_POST['state'].'</p>'."\n";
        $message .= '        <hr />'."\n";
        $message .= '        <pre>'.$_POST['message'].'</pre>'."\n";
        $message .= '    </BODY>'."\n";
        $message .= '</HTML>'."\n";

        $headers = 'MIME-Version: 1.1'."\n";
        $headers .= 'Content-type: text/html; charset=utf-8'."\n";
        $headers .= 'Content-Transfer-Encoding: 8bit'."\n";
        $headers .= 'From: "'.$_POST['name'].'" <'.$_POST['email'].'>'."\n";
        $headers .= 'Reply-To: "'.$_POST['name'].'" <'.$_POST['email'].'>'."\n";
        $headers .= 'Return-Path: '.$_POST['email']."\n";

        if (mail($addr,$subject,$message,$headers))
            echo 'OK';
        else
            echo 'ERROR';
    }
}